package com.cloud.xtools.user.controller.admin;

import com.cloud.xtools.common.annotation.Auth;
import com.cloud.xtools.common.annotation.DisAuth;
import com.cloud.xtools.common.controller.BaseController;
import com.cloud.xtools.common.response.ApiResponse;
import com.cloud.xtools.user.domain.request.admin.UserLoginRequest;
import com.cloud.xtools.user.domain.request.admin.UserRegisterRequest;
import com.cloud.xtools.user.domain.resource.TokenResource;
import com.cloud.xtools.user.domain.resource.admin.UserResource;
import com.cloud.xtools.user.service.admin.AuthService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author Daixinguo
 */
@RestController
@RequestMapping("/api/user/admin")
@RequiredArgsConstructor
@DisAuth
public class AuthController extends BaseController {

    private final AuthService authService;

    /**
     * 注册
     */
    @PostMapping("register")
    public ApiResponse<UserResource> register(@Valid @RequestBody UserRegisterRequest userRegisterRequest) {
        UserResource user = authService.register(userRegisterRequest);
        return created(user);
    }

    // 登录
    @PostMapping("login")
    public ApiResponse<TokenResource> login(@Valid @RequestBody UserLoginRequest userLoginRequest) {
        return success(authService.login(userLoginRequest));
    }

    // 退出登录
    @Auth
    @PostMapping("logout")
    public ApiResponse<Object> logout() {
        authService.logout();
        return noContent();
    }
}
